﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AttributeControl
{
    public partial class AttributeSelector : UserControl
    {
        public event EventHandler SelectedAttributesChanged;
        public void OnSelectedAttributesChanged()
        {
            EventHandler handler = SelectedAttributesChanged;
            if (null != handler) handler(this, EventArgs.Empty);
        }
        
        public class AttributeItem
        {
            public AttributeItem(string tag, Label label, bool selected = false)
            {
                Tag = tag; Selected = selected;
            }
            public Label LabelControl { get; set; }
            public string Tag { get; set; }
            public bool Selected { get; set; }
            public void Toggle()
            {
                if (Selected) { Selected = false; LabelControl.ForeColor = SystemColors.InactiveCaption; }
                else { Selected = true; LabelControl.ForeColor = SystemColors.ControlText; }
            }
        }
        
        private List<AttributeItem> attributes = new List<AttributeItem>();

        public void flpMain_Paint(object sender, EventArgs e)
        {

        }

        public AttributeSelector()
        {
            InitializeComponent();
        }

        private void lblAttribute_Click(object sender, EventArgs e)
        {
            Label currentLabel = (Label)sender;
            string attr = currentLabel.Text;
            AttributeItem currentAttribute = ToggleAttribute(attr);
            if (currentAttribute.Selected)
            {
                currentLabel.ForeColor = SystemColors.ControlText;
            }
            else { currentLabel.ForeColor = SystemColors.InactiveCaption; }
            OnSelectedAttributesChanged();
        }

        public List<AttributeItem> SelectedAttributes
        {
            get { return (attributes.Where(attr => attr.Selected == true).ToList()); }
        }

        public AttributeItem ToggleAttribute(string Attr)
        {
            AttributeItem currentAttribute = attributes.Single(attr => attr.Tag == Attr);
            if (currentAttribute.Selected) {currentAttribute.Selected = false;}
            else {currentAttribute.Selected = true;}
            return currentAttribute;
        }

        public void SetAttributes(string attributeString)
        {
            flpMain.SuspendLayout();
            foreach (string substring in attributeString.Split(','))
            {
                Label lblAttribute = new Label();
                flpMain.Controls.Add(lblAttribute);
                lblAttribute.AutoSize = true;
                lblAttribute.Text = substring;
                lblAttribute.ForeColor = SystemColors.InactiveCaption;
                lblAttribute.Click += new EventHandler(lblAttribute_Click);
                
                attributes.Add(new AttributeItem(substring, lblAttribute));
            }
            flpMain.ResumeLayout();
            flpMain.PerformLayout();
        }

        public void UnselectAll()
        {
            attributes.ForEach(attr => attr.Selected = false);
            foreach (Control control in flpMain.Controls)
            {
                ((Label)control).ForeColor = SystemColors.InactiveCaption;
            }
            OnSelectedAttributesChanged();
        }

        public string AttributeString
        {
            get 
            { 
                string attrString = "";  
                attributes.ForEach(x => attrString += x.Selected ? x.Tag : string.Empty);
                return attrString;
            }
        }
    }
}
